﻿using System;
using System.Net;
using System.Linq;
using Daemon.Model;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using Daemon.Common;
using Daemon.Service.Contract;
using Microsoft.AspNetCore.Authorization;
using Daemon.Common.Exceptions;

namespace DaemonAPI
{
    [ApiController]
    [Route("daemonApi/[controller]/[action]")]
    public class PublicSharedController : BaseApiController<PublicShared, IPublicSharedRepository>
    {
        private readonly ApiDBContent _context;

        public PublicSharedController(IPublicSharedRepository repository,
        IHttpContextAccessor httpContextAccessor,
        ApiDBContent context) : base(repository, httpContextAccessor)
        {
            _context = context;
        }

        [HttpGet]
        [Route("")]
        [AllowAnonymous]
        public ResultModel GetSharedInfo(string uid)
        {
            var sharedInfo = _context.publicShared.FirstOrDefault(r => r.SharedUid == uid);
            if (sharedInfo == null)
            {
                throw new BusinessException((int)BusinessCode.NonExist, "当前连接不存在！");
            }

            if (sharedInfo.EndTime < DateTime.Now)
            {
                throw new BusinessException((int)BusinessCode.NonExist, "当前连接已过期！");
            }

            var studentId = sharedInfo.StudentId;
            var studentInfo = _context.StudentScoreGrid.Where(r => r.Id == studentId);
            return new ResultModel(HttpStatusCode.OK, "", studentInfo);
        }
    }
}
